/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package PropertyRental.war.servlets;

import PropertyRental.ejb.persistence.Owner;
import PropertyRental.ejb.persistence.Property;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Sébastien
 */
public class AddPropertyAction extends Action{
    public AddPropertyAction(String success, String fail) {
        super(success,fail);
    }

    @Override
    public String perform(HttpServletRequest req) {
        HttpSession session = req.getSession();
        Object account = session.getAttribute("account");
        if (account != null){
            if(account instanceof Owner){
                Owner o = (Owner) account;
                try {
                    if (req.getParameter("add") != null) {
                        String type = req.getParameter("type");
                        String adress = req.getParameter("adress");
                        int numberBedroom = Integer.parseInt(req.getParameter("numberBedroom"));
                        int numberBathroom = Integer.parseInt(req.getParameter("numberBathroom"));
                        int numberOtherRoom = Integer.parseInt(req.getParameter("numberOtherRoom"));
                        double rent = Double.parseDouble(req.getParameter("rent"));
                        String location = req.getParameter("location");

                        Property prop = propertyEJB.addProperty(type, adress, numberBedroom, numberBathroom, numberOtherRoom, rent, location);
                        if (prop != null) {
                            ownerEJB.addPropertyToOwner(o, prop);
                            return this.getSuccesspage();
                        }
                    }
                } catch (Exception ex) {
                }
            }
        }
        return this.getFailpage();
    }

}
